package org.wyn.mongodb.dao.impl;

import java.util.List;

import org.bson.types.ObjectId;
import org.wyn.mongodb.bean.Blog;
import org.wyn.mongodb.bean.FreshThing;
import org.wyn.mongodb.bean.FreshThingType;
import org.wyn.mongodb.bean.UserDetail;
import org.wyn.mongodb.common.SearchBDProvider;

import com.google.code.morphia.dao.BasicDAO;
import com.google.inject.Inject;
/**
 * 新鲜事mongodb dao实现层
 * @author wyn
 *
 */
public class FreshThingDao extends BasicDAO<FreshThing,ObjectId> {
	public FreshThingDao(){
		super(SearchBDProvider.getInstance());
	}
	/**
	 * 保存"新鲜事--写日志"
	 * @param blog 日志
	 * @param userName 当事人userName
	 * @param realName 当事人realName
	 * @return
	 */
	public boolean saveThingWriteBlog(Blog blog,String userName,String realName){
		FreshThing ft = FreshThing.buildWriteBlog(blog, userName, realName);
		UserDetail ud = userDetailDao.findOne("userName", userName);
		ft.setPortraitId(ud.getPortrait());
		save(ft);
		ensureIndexes();
		return true;
	}
	/**
	 * 保存"新鲜事--成为好友"
	 * @param userName 当事人账号
	 * @param friendUserName 好友账号
	 * @return
	 */
	public boolean saveThingBecomeFriend(String userName,String friendUserName){
		String realName = userDao.getRealName(userName);
		String friendRealName = userDao.getRealName(friendUserName);
		FreshThing ft = FreshThing.buildBecomeFriend(userName, realName, friendUserName, friendRealName);
		UserDetail ud = userDetailDao.findOne("userName", userName);
		ft.setPortraitId(ud.getPortrait());
		save(ft);
		ensureIndexes();
		return true;
	}
	/**
	 * 保存"新鲜事--上传图片"
	 * @param userName 当事人账号
	 * @param albumId 相册id
	 * @param imageIds 图片id列表
	 * @return
	 */
	public boolean saveThingUploadImage(String userName,ObjectId albumId,List<ObjectId> imageIds){
		if(null == imageIds || imageIds.size() == 0)
			return false;
		String albumName = albumDao.getAlbumName(albumId);
		if(null == albumName)
			return false;
		String realName = userDao.getRealName(userName);
		FreshThing ft = FreshThing.buildUploadImage(userName, realName, albumId, albumName, imageIds);
		UserDetail ud = userDetailDao.findOne("userName", userName);
		ft.setPortraitId(ud.getPortrait());
		save(ft);
		ensureIndexes();
		return true;
	}
	/**
	 * 保存"新鲜事--发表状态"
	 * @param userName
	 * @param content
	 * @return
	 */
	public boolean saveThingPublishStatus(String userName,String content){
		UserDetail ud = userDetailDao.findOne("userName", userName);
		FreshThing ft = new FreshThing(userName, ud.getRealName(), FreshThingType.PUBLISHSTATUS);
		ft.setPortraitId(ud.getPortrait());
		ft.setStatusContent(content);
		save(ft);
		ensureIndexes();
		return true;
	}
	/**
	 * 保存"新鲜事--修改头像"
	 * @param userName 当事人账号
	 * @param portraitId 新修改的头像图片id
	 * @return 成功保存true失败false
	 */
	public boolean saveThingModifyPortrait(String userName,ObjectId portraitId){
		String realName = userDao.getRealName(userName);
		FreshThing ft = FreshThing.buildModifyPortrait(userName, realName, portraitId);
		UserDetail ud = userDetailDao.findOne("userName", userName);
		ft.setPortraitId(ud.getPortrait());
		save(ft);
		ensureIndexes();
		return true;
	}
	@Inject
	private UserDao userDao;
	@Inject
	private AlbumDao albumDao;
	@Inject
	private UserDetailDao userDetailDao;
	public UserDao getUserDao() {
		return userDao;
	}
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	public AlbumDao getAlbumDao() {
		return albumDao;
	}
	public void setAlbumDao(AlbumDao albumDao) {
		this.albumDao = albumDao;
	}
	public UserDetailDao getUserDetailDao() {
		return userDetailDao;
	}
	public void setUserDetailDao(UserDetailDao userDetailDao) {
		this.userDetailDao = userDetailDao;
	}
	public static void main(String[] args) {
		FreshThingDao dao = new FreshThingDao();
		FreshThing thing = new FreshThing();
		thing.setType(FreshThingType.PUBLISHSTATUS);
		//dao.save(thing);
		thing = dao.findOne("type", FreshThingType.PUBLISHSTATUS);
		System.out.println(thing.getType());
	}
}
